package com.yswee.mallnavi.view.member;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.clickedtab.ClickedTabBar;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.InfoEntity;

import java.util.ArrayList;


/***
 * TabBar
 */
public class TabBar extends ClickedTabBar {

    @ViewInject
    private LinearLayout llcontent;

    public TabBar(Context context) {
        super(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.view_info_member_tabbar;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected int[] getChildViewResIDs() {
        return new int[] {
        };
    }

    public void setDataArray(ArrayList<InfoEntity> array) {
        llcontent.removeAllViews();
        if(array != null && array.size() > 0) {
            childviewList = new View[array.size()];
            for(int i = 0; i < array.size(); i++) {
                Button button = (Button) LayoutInflater.from(mContext).inflate(R.layout.view_info_member_tabbar_item, null, false);
                button.setText(array.get(i).name);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                if (i > 0) {
                    layoutParams.leftMargin = (int) mContext.getResources().getDimension(R.dimen.dp_15);
                }
                llcontent.addView(button, layoutParams);
                childviewList[i] = button;
                if (button != null){
                    final int index = i;
                    button.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(null != listener) {
                                listener.onTabChanged(index);
                            }
                            setTabIndex(index);
                        }
                    });
                }
            }
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void focusChildView(int index, View v) {
        Button button = (Button) v;
        button.setBackgroundResource(R.drawable.widget_mbutton_bg);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void unfocusChildView(int index, View v) {
        Button button = (Button) v;
        button.setBackgroundResource(R.drawable.widget_mbutton_border_bg);
    }

}